// pages/weixiu/weixiu.js
import {
  getHouse,
} from '../../api/baoxiu'
import {
  postVisitor
}
from '../../api/addInvitation'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    show1: false,
    actions: [{
      name: '着色选项'
    }],
    select: {
      name: '请选择报修房屋'
    },
    mobile: '',
    date: '选择上门维修日期',
    name: '',
    gender: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    wx.getStorage({
      key: 'token',
      async success(resToken) {
        await getHouse({
          token: resToken.data
        }).then(res => {
          that.setData({
            actions: res.data.data
          })
        }).catch(res => {
          console.log(res);
        })
      },
      fail() {
        wx.redirectTo({
          url: '/pages/login/login',
        })
      }
    })
  },
  onSelect(event) {
    this.setData({
      select: event.detail
    })
  },

  showPopup() {
    this.setData({
      show: true
    });
  },
  showPopup1() {
    this.setData({
      show1: true
    });
  },
  onDisplay() {
    this.setData({
      show1: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirm(event) {
    this.setData({
      show1: false,
      date: this.formatDate(event.detail),
    });
  },
  onClose1(e) {
    this.setData({
      show1: false
    });
  },

  onChange(e) {
    this.setData({
      mobile: e.detail
    })
    console.log(this.data.mobile);
  },
  onChange1(event) {
    this.setData({
      problem: event.detail
    })
  },
  getName(e) {
    console.log(e.detail.value);
    this.setData({
      name: e.detail.value
    })
  },
  getMobile(e) {
    console.log(e.detail.value);
    this.setData({
      mobile: e.detail.value
    })
  },
  bindchange(e) {
    this.setData({
      gender: e.detail.value
    })
  },
  submit() {
    let data = this.data
    let obj = {
      houseId: data.select.id,
      name: data.name,
      gender: data.gender,
      mobile: data.mobile,
      visitDate: data.date
    }
    console.log(data);
    if(!obj.houseId)return wx.showToast({
      title: '请选择房屋',
      icon:'none'
    })
    let nameZe = /^[\u4E00-\u9FA5]{2,15}$/
    if (!nameZe.test(obj.name)) return wx.showToast({
      title: '请输入正确的姓名',
      icon: 'none'
    })
    if (!this.checkPhone(obj.mobile)) return wx.showToast({
      title: '请输入正确的手机号',
      icon: 'none'
    })
    if(obj.date === "选择上门维修日期")return wx.showToast({
      title: '请选择访问日期',
      icon:"none"
    })
    // if (id) obj.id = data.id
    wx.getStorage({
      key: 'token',
      async success(res) {
        await postVisitor({
          token: res.data,
          data: obj
        }).then(res => {
          
          if (res.data.code === 10000) {
            console.log(res);
            wx.showToast({
              title: '添加成功'
            })
            setTimeout(() => {
              wx.navigateBack({
                url: '/pages/index/index.js'
              })
            }, 2000);
          }
        }).catch(res=>{
          console.log(res);
        })
      }
    })
  },
  checkPhone(mobile) {
    let RegExp = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    return RegExp.test(mobile)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})